load_clean_eb <- function(path){
  haven::read_dta(path) |> 
    select("YEAR":"WEIGHTS","OLD15") |> 
    filter(EDUC<98) |> 
    reframe(ms = as.character(haven::as_factor(NATION)),
            year = YEAR, weights = WEIGHTS,
            old15 = OLD15,
            proeu = proeu,    
            left_right = left_right,
            left_right_sq = scale(I(left_right^2)),
            education = as.character(haven::as_factor(EDUC)),
            pol_disc = pol_disc -1, # recode so that frequent politics discussion is 0
            sex = as.factor(haven::as_factor(SEX)),
            age = as.numeric(haven::as_factor(AGE)),
            age_sq = scale(I(age^2)),
            education = as.factor(ifelse(education == "22 OR OLDER" | education =="STILL STUDYING" ,
                              "University", "High School")),
            sex = case_when(sex == "MALE" ~ " Male",
                                sex == "FEMALE" ~ "Female",
                                .default = NA),
            identity = identity-1 # set national to zero
            ) -> outdata
}

eb_models <- function(eb_data){
  year <- as.vector(unique(eb_data$year))
  future.apply::future_lapply(seq(year), function(i) {
  fixest::feols(proeu ~ left_right |ms, data = eb_data, 
                subset = eb_data$year == year[i],
                weights = eb_data$weights,
                cluster = "ms") |> 
    broom::tidy() 
}) |> 
    bind_rows() |> 
    mutate(year = year) -> proeu_model
}

eb_control_models <- function(eb_data){
  year <- as.vector(unique(eb_data$year))
  future.apply::future_lapply(seq(year), function(i) {
    fixest::feols(proeu ~ left_right + left_right_sq + left_right:pol_disc + left_right_sq:pol_disc + pol_disc + sex + education + age + age_sq |ms, 
                  data = eb_data, 
                  subset = eb_data$year == year[i],
                  cluster = "ms") |> 
      broom::tidy() 
  }) |> 
    bind_rows() |> 
    filter(term == "left_right" | term == "left_right_sq") |> 
    mutate(year = rep(year, each = 2))
  
}

eb_subgroup_models <- function(eb_data){
  year <- as.vector(unique(eb_data$year))
  eb_data |>
    mutate(lr_groups = case_when( eb_data$left_right > 8 ~ "Radical Right",
               eb_data$left_right > 6 ~ "Center Right",
               eb_data$left_right > 4 ~ "Center",
               eb_data$left_right > 2 ~ "Center Left",
               eb_data$left_right <= 2 ~ "Radical Left")) -> eb_data
    
    future_lapply(seq(year), function(i) {
    fixest::feols(proeu ~ lr_groups + lr_groups: pol_disc  +  pol_disc + sex + education + age + age_sq |ms, data = eb_data, 
                  subset = eb_data$year == year[i],
                  cluster = "ms") |> 
      broom::tidy() 
  }) |> 
    bind_rows() |> 
    filter(term == "lr_groupsCenter Left" |
          term == "lr_groupsCenter Right" |
          term == "lr_groupsRadical Left"| 
          term == "lr_groupsRadical Right") |>
      mutate(year = rep(year, each = 4)) -> tmp_data
    
    tmp_data$term <- case_when(tmp_data$term == "lr_groupsRadical Left" ~ "Radical Left",
                              tmp_data$term == "lr_groupsCenter Left" ~ "Center Left",
                              tmp_data$term == "lr_groupsCenter Right" ~ "Center Right",
                              tmp_data$term == "lr_groupsRadical Right" ~ "Radical Right")
    tmp_data$groups <- ordered(tmp_data$term, levels = c("Radical Left", "Center Left", 
                                                       "Center Right", "Radical Right"))
    
    tmp_data |>
      arrange(groups) |> 
      group_by(groups) 
}

eb_plot <- function(eb_model) {
  ggplot(eb_model, aes(x = year, y = estimate)) +
    geom_errorbar(aes(ymin = estimate - 1.96*std.error, 
                               ymax = estimate + 1.96*std.error), 
                  width = 0.2) +
    geom_point() +
    theme_Publication() +
    labs(y = "Effect of Left-Right Selfplacement", 
         title = "Left-Right Self-placement on Pro-EU Attitudes over Time")
}

eb_plot_cont <- function(eb_controls) {
  ggplot(eb_controls, aes(x = year, y = estimate, col = term)) +
    geom_errorbar(aes(ymin = estimate - 1.96*std.error, 
                               ymax = estimate + 1.96*std.error), width = .2,
                  position=position_dodge(0.5)) +
    geom_point( position=position_dodge(0.5)) +
    theme_Publication() +
    labs(y = "Effect of Left-Right Selfplacement", title = "Left-Right Self-placement on Pro-EU Attitudes over Time")
}

eb_plot_subgroups <- function(eb_group_models) {
  eb_group_models |>
  ggplot(aes(x = year, y = estimate, col = groups)) +
    geom_errorbar(ggplot2::aes(ymin = estimate - 1.96*std.error, 
                               ymax = estimate + 1.96*std.error), width = .2,
                  position=position_dodge(0.5)) +
    geom_point( position=position_dodge(0.5)) +
    geom_abline(slope = 0, intercept = 0, linetype = "dashed") +
    theme_Publication() +
    facet_wrap(~groups, ncol = 2) +
    labs(y = "Effect of Left-Right Selfplacement", title = "Left-Right Self-Placement on Pro-EU Attitudes over Time",
         subtitle = "Note: Center is the reference category")}
